<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width = device-width , initial-scale = 1 , minimum-scale = 1 , maximum-scale = 1 , user-scalable = no">
    <link rel="stylesheet" href="../../resource/css/bootstrap.min.css">
    <link rel="stylesheet" href="../../resource/css/common.css">
</head>
<body class="bg-color-1">
<div id="header" class="bg-color-2">
    <a href="javascript:void(0)" class="nav-back">返回</a>
    发布优惠券
    <span class="btn-user-set"></span>
</div>
<div class="coupon bg-color-2">
 <!--   <div class="coupon-left">
        <img class="seller-img" src="../../resource/images/seller-demo.png" />
    </div>
    <div class="coupon-detail">
        <strong class="seller-name">坑德基(sdhsds )</strong>
        <strong class="seller-sale">7折</strong>
        <span class="seller-tag seller-detail">西餐 快餐</span>
        <span class="seller-dis seller-detail">500m</span>
        <span class="coupon-time seller-detail">12.31.-01-01</span>
    </div>
    <h1 class="code-describe">如果该优惠券被领取你将获得<strong class="code-count">50</strong>积分</h1>-->
</div>
<div class="coupon-dec-box">
    <span class="coupon-describe">描述该优惠券您可获得额外<strong class="code-count">5</strong>积分<br>例如：这家店环境好，性价比高，值得一试</span>
    <textarea class="coupon-describe" ></textarea>
    <button class="coupon-btn-release">发 布</button>
</div>

<!--模态框 start-->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                是否发布该优惠券<br>
                优惠券被领取将获得积分
            </div>
            <div class="modal-footer">
                <button type="button" class="sanFast-btn back"
                        data-dismiss="modal">返回
                </button>
                <button type="button" class="sanFast-btn release">
                    是
                </button>
            </div>
        </div>
    </div>
</div>
<!--模态框 end-->

<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="../../resource/js/jquery.min.js"></script>

<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="../../resource/js/bootstrap.min.js"></script>
<script src="../../resource/js/commonjs.js"></script>
<script type="text/javascript">
    $(function() {
        isLogin();
        getCoupon();
        $('#myModal').modal();
        eventBinding();
    });
    /* 获取需要发布的coupon信息 */
    function getCoupon() {
        var url = "/UserCenter/getPublishCouponInfo";
        var arg = {};
        arg.coupon_code = getArg("code");
        arg.longitude = getArg("longitude");
        arg.latitude = getArg("latitude");
        ajaxJsonp(url, arg, renderCoupon);
    }
    /* 渲染coupon */
    function renderCoupon(res) {
        var html = '';
        data = res.data;
        var distance = data.distance.toFixed(1);
        var stt = new Date(data.start_time*1000).toLocaleDateString();
        var ext = new Date(data.expired_time*1000).toLocaleDateString();
        stt = stt.replace(/\//g,".");
        ext = ext.replace(/\//g,".");

        if(data.distance < 1) {
            distance *= 1000;
            distance += 'm';
        } else {
            distance += 'km';
        }
        html += '<div class="coupon-left">';
        html += '<img class="seller-img" src="'+ data.image +'"/>';
        html += '</div>';
        html += '<div class="coupon-detail">';
        html += '<strong class="seller-name">' + data.shop_name + '</strong>';
        html += '<strong class="seller-sale">' + data.discount + '折</strong>';
        html += '<span class="seller-tag seller-detail">' + data.tag + '</span>';
        html += '<span class="seller-dis seller-detail">' + distance + '</span>';
        html += '<span class="coupon-time seller-detail">' + stt + '-' + ext + '</span>';
        html += '</div>';
        html += '<h1 class="code-describe">如果该优惠券被领取你将获得<strong class="code-count">'+ data.receive_credit +'</strong>积分</h1>';

        $(".coupon").append(html);
    }
    /* 发布优惠券 */
    function releaseCoupon() {
        var url = "/UserCenter/publishCoupon";
        var arg = {};
        var remark = $(".coupon-describe").val()
        if(remark.length > 0) {
            arg.remark = remark;
        }
        arg.coupon_code = getArg("code");
        ajaxJsonp(url, arg, releaseCouponSuc);
    }
    /* 发布成功 */
    function releaseCouponSuc(res) {
        var msg = res.status.msg;
        showModal(msg);
        var url = "userInfo.html";
        doJump(url);
    }
    /* user part event binding */
    function eventBinding() {
        $(".nav-back").click(function(){
            location.href = "userInfo.html?url=1";
        })
        $('.modal-footer .back').click(function() {
            history.go(-1);  //***
        });
        $('.modal-footer .release').click(function() {
            $('#myModal').modal('hide');
            var url = location.href;
            var code = getArg("code");
            /*  */
            $('.coupon-dec-box').fadeIn();
        });
        $("span.coupon-describe").click(function() {
            $(this).hide();
            $("textarea.coupon-describe").css('display','block');
            $("textarea.coupon-describe").show().focus();
        });
        $("textarea.coupon-describe").blur(function() {
            if(!$(this).val()) {
                $("span.coupon-describe").show();
                $(this).hide();
            }
        });
        $('.btn-user-set').click(function() {
            location.href = "userInfoSetting.html?url=1";
        });
        $('.coupon-btn-release').click(function() {
            releaseCoupon();
        });
        $(".user-info-detail").click(function() {
            location.href = "userCodeInfo.htmlurl=1";
        });
    }

</script>
</body>
</html>